package top.wardyan.wtask.jobs;

import java.sql.Timestamp;

import com.jfinal.aop.Aop;
import com.jfinal.plugin.activerecord.Db;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import top.wardyan.base.common.util.DateUtil;
import top.wardyan.wtask.tapd.api.TapdBusiness;

public class TapdDownloadJob {

	private static final cn.hutool.log.Log log = cn.hutool.log.Log.get();
	
	private void deal() {
		Integer second = RandomUtil.randomInt(10, 10000);
		ThreadUtil.sleep(second);
		TapdBusiness tapdBusiness = Aop.get(TapdBusiness.class);
		Timestamp begin = DateUtil.range(17);
		Timestamp end = DateUtil.range(2);
		tapdBusiness.fetchStory(begin, end);
		tapdBusiness.fetchTask(begin, end);
	}
	public void dealA() {
		deal();
	}
	public void dealB() {
		deal();
	}
	public void dealC() {
		deal();
	}
	public void dealD() {
		deal();
	}

	public void dealE() {
		deal();
	}

	public void dealF() {
		deal();
	}
	
//	/**
//	 * 删除最近30天创建的任务, 重导最近40天的
//	 */
//	public void reimportmonth() {
//
//		Integer second = RandomUtil.randomInt(10, 10000);
//		ThreadUtil.sleep(second);
//		
//		TapdBusiness tapdBusiness = Aop.get(TapdBusiness.class);
//		Timestamp begin = DateUtil.range(15);
//		Timestamp end = DateUtil.range(2);
//		
//		tapdBusiness.fetchStory(begin, end);
//		tapdBusiness.fetchTask(begin, end);
//		
//		// 上面已经重新拉取了最近1个月的数据了, 然后检查刚刚没刷新过的数据
//		tapdBusiness.yp_tapd_dealdeleted();
//		
//	}


	/**
	 * 每周一中午11点 重导过去一个月数据,其中最近2周的数据先删再重新导入, 避免在TAPD删掉的任务
	 */
	public void reimport() {
		log.info("每周一中午11点 重导过去一个月数据,其中最近3周的数据先删再重新导入, 避免在TAPD删掉的任务");
		TapdBusiness tapdBusiness = Aop.get(TapdBusiness.class);
		// 过去2周
		Timestamp begin = new Timestamp(DateUtil.range(17).getTime() - 1000 * 60 * 60 * 24 * 7 * 2);
		Timestamp end = DateUtil.range(2);

		Db.delete("delete from yt_tapd_task where sCreateDate >= ?", begin);
		Db.delete("delete from yt_tapd_story where sCreateDate >= ?", begin);
		
		begin = DateUtil.range(15);
		tapdBusiness.fetchStory(begin, end);
		tapdBusiness.fetchTask(begin, end);
	}

}
